package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    AttrService attrService;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public void addRelation(List<AttrGroupRelationVo> vos) {
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map((obj) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(obj, attrAttrgroupRelationEntity);

            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());

        this.saveBatch(collect);
    }

    @Override
    public void saveDetail(AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntity) {
        ArrayList<AttrAttrgroupRelationEntity> objects = new ArrayList<>();
        for (AttrAttrgroupRelationEntity attRelation:
             attrAttrgroupRelationEntity) {
            objects.add(attRelation);
        }

        this.saveBatch(objects);
    }

    /**
     * 获取分类的属性分组下的所有属性
     *
     *
     * @param cateId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttr(Long cateId) {
        List<AttrGroupEntity> relationEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cateId));
        List<AttrGroupWithAttrVo> collect = relationEntities.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrVo);
            List<AttrEntity> attrList = attrService.queryRelationAttr(attrGroupEntity.getAttrGroupId());
            attrGroupWithAttrVo.setAttrs(attrList);
            return attrGroupWithAttrVo;
        }).collect(Collectors.toList());

        return collect;
    }

}